#!/usr/bin/env python3
# -*- coding: utf-8 -*-


from flask import Blueprint, request
from database import con
from .lib import *


sql = Blueprint('sql', __name__)

@sql.route('/select',methods=['GET', 'POST'])
def select():
    try:
        cur = con.cursor()
        
        data = request_parse(request)
        sql = data["sql"]
        cur.execute(sql)
        result = cur.fetchall()
        return {
            "code":0,
            "data":result 
        }
    except  Exception as e:
        print(e)
        return {
            "code":-1,
            "msg":str(e)
        }
    finally:
        cur.close()

@sql.route('/update',methods=['GET', 'POST'])
def update():
    try:
        data = request_parse(request)
        sql = data["sql"]
        cur = con.cursor()
        cur.execute(sql)
        result={"count":cur.rowcount}
        con.commit()
        return {
            "code":0,
            "data":result 
        }
    except  Exception as e:
        print(e)
        return {
            "code":-1,
            "msg":str(e)
        }
    finally:
        cur.close()
    
